"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下,备机prepare语句对unlogged的数据同步
Create At   : 2025-03-07
Owner       : zhongchengzhi
Description :
    1、查询集群状态
    2、主机创建unlogged表
    3、主机向unlogged表插入数据
    4、备机准备和执行prepare语句
    5、主机修改表信息
    6、备机执行prepare语句
    7、主机删除表信息
    8、备机执行prepare语句
    9、清理数据
    10、恢复环境

Expect      :
    1、查询结果正常
    2、创建成功
    3、插入数据成功
    4、执行成功
    5、修改数据成功
    6、执行成功
    7、删除成功
    8、执行成功
    9、清理数据成功
    10、恢复环境成功
History     :
    1、2025-03-07 创建
    2、2025-07-12 改造用例为1p1s环境可执行
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_ROOTSH = CommonSH('PrimaryRoot')
Primary_SH = CommonSH('PrimaryDbUser')
Standby1_SH = CommonSH('Standby1DbUser')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:主机创建unlogged表;expect:成功-----'
        self.log.info(text)
        sql = f'''
                drop table if exists test_policy6 cascade;
                create unlogged table test_policy6(id int,usr name);
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('CREATE TABLE', status, '执行失败' + text)

        text = '-----step3:主机向unlogged表插入数据;expect:成功-----'
        self.log.info(text)
        sql = f'''
                insert into test_policy6(id, usr) values(1, 'test_user_001');
                insert into test_policy6(id, usr) values(2, 'test_user_002');
                insert into test_policy6(id, usr) values(3, 'test_user_002');
                insert into test_policy6(id, usr) values(4, 'test_user_002');
                insert into test_policy6(id, usr) values(5, 'test_user_001');
                insert into test_policy6(id, usr) values(5, 'test_user_003');
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('INSERT 0 1\nINSERT 0 1\nINSERT 0 1\nINSERT 0 1\nINSERT 0 1\nINSERT 0 1', status, '执行失败' + text)

        text = '-----step3:备机准备和执行prepare语句;expect:执行成功-----'
        self.log.info(text)
        sql = f'''
                prepare standby1 as select * from test_policy6;
                explain execute standby1(1);
                '''
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('Seq Scan on test_policy6  (cost=0.00..18.33 rows=833 width=68)', status, '执行失败' + text)

        text = '-----step4:主机修改表信息;expect:成功-----'
        self.log.info(text)
        sql = f"update test_policy6 set id = id + 1;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('UPDATE 6', status, '执行失败' + text)

        text = '-----step3:备机执行prepare语句;expect:执行成功-----'
        self.log.info(text)
        sql = f'''
                prepare standby1 as select * from test_policy6;
                explain execute standby1(1);
                '''
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('Seq Scan on test_policy6  (cost=0.00..18.33 rows=833 width=68)', status, '执行失败' + text)

        text = '-----step6:主机删除表信息;expect:成功-----'
        self.log.info(text)
        sql = f"delete from test_policy6;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('DELETE 6', status, '执行失败' + text)

        text = '-----step3:备机执行prepare语句;expect:执行成功-----'
        self.log.info(text)
        sql = f'''
                prepare standby1 as select * from test_policy6;
                explain execute standby1(1);
                '''
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('Seq Scan on test_policy6  (cost=0.00..18.33 rows=833 width=68)', status, '执行失败' + text)

        text = '-----step8:清理环境;expect:成功-----'
        self.log.info(text)
        sql = f'''
                drop table test_policy6;
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('DROP TABLE', status, '执行失败' + text)

    def tearDown(self):
        text = '-----step9:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')